package com.ztmao.shortlink.admin.controller;

import com.ztmao.shortlink.admin.convention.result.Result;
import com.ztmao.shortlink.admin.convention.result.Results;
import com.ztmao.shortlink.admin.dto.req.UserLoginReqDTO;
import com.ztmao.shortlink.admin.dto.req.UserRegisterReqDTO;
import com.ztmao.shortlink.admin.dto.req.UserUpdateReqDTO;
import com.ztmao.shortlink.admin.dto.resp.UserLoginRespDTO;
import com.ztmao.shortlink.admin.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 *用户控制器
 */
@RestController
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;
    /**
     * 获取用户信息通过用户名
     * @param username
     * @return {@link String }
     */
    @GetMapping("/api/short-link/admin/v1/user/{username}")
    public Result getUserByUserName(@PathVariable("username") String username){
       return userService.getUserByUserName(username);
    }

    /**
     * 用户名是否存在
     * @param username 用户名
     * @return {@link Result }<{@link Boolean }>
     */
    @GetMapping("/api/short-link/admin/v1/has-username")
    public Result<Boolean> hasUserName(@RequestParam("username") String username){
        return Results.success(userService.hasUserName(username));
    }

    /**
     * 用户注册
     * @param userRegisterReqDTO 用户注册请求参数
     *
     */
    @PostMapping("/api/short-link/admin/v1/user")
    public Result<Void> register(@RequestBody UserRegisterReqDTO userRegisterReqDTO){
       return userService.register(userRegisterReqDTO);
    }
    /**
     * 修改用户
     */
    @PutMapping("/api/short-link/admin/v1/user")
    public Result<Void> update(@RequestBody UserUpdateReqDTO requestParam) {
        userService.update(requestParam);
        return Results.success();
    }
    /**
     * 用户登录
     */
    @PostMapping("/api/short-link/admin/v1/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParam) {
        return Results.success(userService.login(requestParam));
    }

    /**
     * 检查用户是否登录
     */
    @GetMapping("/api/short-link/admin/v1/user/check-login")
    public Result<Boolean> checkLogin(@RequestParam("username") String username, @RequestParam("token") String token) {
        return Results.success(userService.checkLogin(username, token));
    }

    /**
     * 用户退出登录
     */
    @DeleteMapping("/api/admin/short-link/v1/user/logout")
    public Result<Void> logout(@RequestParam("username") String username, @RequestParam("token") String token) {
        userService.logout(username, token);
        return Results.success();
    }

}
